﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using XFControls;

namespace XFControls.XFPanels.XFPanelItems
{
    class XFItemBack : IXFItem
    {
        private int height = 40;

        public XFItemBack()
        {
            Style = new XFItemStyle()
            {
                PaddingTop = 2
            };
            Style.BackgroundColor = Color.RoyalBlue;
            Style.TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
            Style.TextColor = Color.White;
            Style.DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash;

            ItemType = XFItemType.Clickable;
        }

        #region IXFItem Members

        public XFItemType ItemType { get; set; }
        public XFPanelBase Parent { get; set; }
        public XFItemState State { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }

        public int GetHeight()
        {
            return height + Style.PaddingTop + Style.PaddingBottom;
        }

        public void ResetHeight() { }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Parent == null ? Screen.PrimaryScreen.WorkingArea.Width : Parent.Width;
            string loading = "Back";

            if (Parent != null)
            {
                if (Parent.AlphaBarWidth > 0)
                {
                    width -= Parent.AlphaBarWidth;

                    if (Parent.ScrollbarVisible)
                        width -= 5;
                }
            }

            int xmid = width / 2;

            XFControlUtils.DrawBackground(Style, g, x, y, width, height);
            XFControlUtils.DrawBoarders(Style, g, x, y, width, height);

            using (SolidBrush b = new SolidBrush(Style.TextColor))
            {
                xmid -= (int)(g.MeasureString(loading, Style.TextFont).Width / 2);
                g.DrawString(loading, Style.TextFont, b, xmid, y +Style.PaddingTop);
            }
        }

        public XFItemClickResult GetClickResult()
        {
            if (Parent == null)
                return XFItemClickResult.Nothing;

            return XFItemClickResult.SlideRight;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();
            if (Style != null)
                Style.Dispose();

            Style = null;
            Buffer = null;
        }

        #endregion
    }
}
